<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="成品id" prop="sfproductId">-->
<!--        <el-input-->
<!--          v-model="queryParams.sfproductId"-->
<!--          placeholder="请输入成品id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="成品名称" prop="sfproductName">
        <el-input
          v-model="queryParams.sfproductName"
          placeholder="请输入成品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="ifSelfProduced">
        <el-select v-model="queryParams.ifSelfProduced" placeholder="类型" clearable>
          <el-option
            v-for="dict in dict.type.lw_self_produced"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="数量" prop="quantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantity"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fpWarehouse:fpWarehouseWl:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fpWarehouse:fpWarehouseWl:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fpWarehouse:fpWarehouseWl:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fpWarehouse:fpWarehouseWl:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fpWarehouseWlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="成品库存id" align="center" prop="id" />-->
<!--      <el-table-column label="成品id" align="center" prop="sfproductId" />-->
      <el-table-column label="成品名称" align="center" prop="sfproductName" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="参数" align="center" prop="parameter" />
      <el-table-column label="类型" align="center" prop="ifSelfProduced">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lw_self_produced" :value="scope.row.ifSelfProduced"/>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-2"
            @click="outStorage(scope.row)"
            v-hasPermi="['fpWarehouse:fpWarehouse:out']"
          >出库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-full"
            @click="putStorage(scope.row)"
            v-hasPermi="['fpWarehouse:fpWarehouse:put']"
          >入库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fpWarehouse:fpWarehouseWl:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['fpWarehouse:fpWarehouseWl:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网联成品仓库管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="成品ID" prop="sfproductId">
          <el-select v-model="form.sfproductId" filterable placeholder="请选择" :disabled="disableSfproductId">
            <el-option
              v-for="(item, index) in sfproducts"
              :key="item.sfproductId "
              :label="item.model + ' - ' + sfproductNames[index]"
              :value="item.sfproductId">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="成品名称" prop="sfproductName">-->
<!--          <el-input v-model="form.sfproductName" placeholder="请输入成品名称" />-->
<!--        </el-form-item>-->
        <el-form-item label="类型" prop="ifSelfProduced">
          <el-radio-group v-model="form.ifSelfProduced">
            <el-radio
              v-for="dict in dict.type.lw_self_produced"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="数量" prop="quantity">-->
<!--          <el-input v-model="form.quantity" placeholder="请输入数量" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 出入库对话框 -->
    <el-dialog :title="title" :visible.sync="openInventory" width="500px" append-to-body>
      <el-form ref="inventory" :model="inventory" :rules="rules" label-width="80px">
        <el-form-item label="成品" prop="sfproductName" >
          <el-input v-model="inventory.sfproductName" :disabled = "true" />
        </el-form-item>
        <el-form-item label="库存" prop="quantity">
          <el-input v-model="inventory.quantity" :disabled = "true"/>
        </el-form-item>
        <el-form-item label="类型" prop="outboundType" v-if="!flash">
          <el-radio-group v-model="inventory.outboundType">
            <el-radio
              v-for="dict in dict.type.lw_outbound_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="入库" prop="variable" v-if="flash">
          <el-input v-model="inventory.variable" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="出库" prop="remove" v-if="!flash">
          <el-input v-model="inventory.remove" placeholder="请输入出库库数量" />
        </el-form-item>
        <el-form-item label="客户" prop="client" v-if="!flash">
          <el-input v-model="inventory.client" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="销售" prop="salesman" v-if="!flash">
          <el-input v-model="inventory.salesman" placeholder="请输入销售" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="inventory.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInventory">操作</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listFpWarehouseWl,
  getFpWarehouseWl,
  delFpWarehouseWl,
  addFpWarehouseWl,
  updateFpWarehouseWl,
  updateFpWarehouseWlQuantity
} from "@/api/warehouse/fpWarehouseWl";
import {listSfproduct} from "@/api/sfproduct/sfproduct";
import {updateFpWarehouseQuantity} from "@/api/warehouse/fpWarehouse";

export default {
  name: "FpWarehouseWl",
  dicts: ['lw_self_produced','lw_outbound_type'],
  data() {
    return {
      flash: '', // 初始化flash属性为空字符串
      disableSfproductId: false,
      sfproducts:[],
      sfproductNames: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网联成品仓库管理表格数据
      fpWarehouseWlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInventory: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sfproductId: null,
        sfproductName: null,
        model:null,
        ifSelfProduced: null,
        quantity: null,
      },
      // 表单参数
      form: {},
      inventory: {},
      // 表单校验
      rules: {
        ifSelfProduced: [
          { required: true, message: "是否自产(1外购 0自产 )不能为空", trigger: "change" }
        ],
        outboundType:[
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSfProductList();

  },
  methods: {
    /** 查询半成品列表 */
    getSfProductList() {
      this.loading = true;
      this.queryParams.pageSize = 1000;//查询列表大小
      listSfproduct(this.queryParams).then(response => {
        this.sfproducts = response.rows;
        this.sfproductNames = response.rows.map(product => product.sfproductName);
        this.loading = false;
      });
    },
    /** 入库按钮操作 */
    putStorage(row) {
      this.reset();
      this.flash = true;
      const sfpId = row.id;
      getFpWarehouseWl(sfpId).then(response => {
        this.inventory = response.data;
        this.inventory.remove = 0;
        this.openInventory = true;
        this.inventory.remark = null;
        this.title = "成品入库";
      });
    },
    /** 出库按钮操作 */
    outStorage(row) {
      this.reset();
      this.flash = false;
      const sfpId = row.id;
      getFpWarehouseWl(sfpId).then(response => {
        this.inventory = response.data;
        this.inventory.variable = 0;
        this.openInventory = true;
        this.inventory.remark = null;
        this.title = "成品出库";
      });
    },
    /** 出入库按钮 */
    submitInventory() {
      this.$refs["inventory"].validate(valid => {
        if (valid) {
          if (this.inventory.id != null) {
            updateFpWarehouseWlQuantity(this.inventory).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.openInventory = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 查询网联成品仓库管理列表 */
    getList() {
      this.loading = true;
      listFpWarehouseWl(this.queryParams).then(response => {
        this.fpWarehouseWlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInventory =false;
      this.disableSfproductId = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sfproductId: null,
        sfproductName: null,
        model:null,
        parameter: null,
        ifSelfProduced: null,
        quantity: null,
        remark: null
      };
      this.resetForm("form");
      this.inventory = {
        id: null,
        sfproductId: null,
        sfproductName: null,
        model:null,
        parameter: null,
        quantity: null,
        remark: null,
        variable:0,
        remove: 0,
        client:null,
        salesman:null,
        outboundType:null,
      };
      this.resetForm("inventory");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网联成品仓库管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFpWarehouseWl(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改网联成品仓库管理";
      });
      this.disableSfproductId = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFpWarehouseWl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.quantity = 0;
            addFpWarehouseWl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除网联成品仓库管理编号为"' + ids + '"的数据项？').then(function() {
        return delFpWarehouseWl(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fpWarehouse/fpWarehouseWl/export', {
        ...this.queryParams
      }, `fpWarehouseWl_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
